﻿using Ldt.AnalysisLabel;
using Ldt.SettingCenter.Core;
using LDT.Task.JudgetDoc.Generation.DataBase;
using LdtCommonFreeSql;
using LdtCommonLibraryHttp;
using LdtCommonLibray.AddressExtract;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Threading.Tasks;

namespace LdtJudge.DataEngine.Server
{
    public class Program
    {
        /// <summary>
        /// 主入口
        /// </summary>
        /// <param name="args">参数</param>
        public static void Main(string[] args)
        {
            var t = CreateHostBuilder(args).Build();
            LdtJudge.DataEngine.CallInterface.CommHelp.FreeSQLHelp.freesqlDb = t.Services.GetService<FreesqlDb>();
            DbHelper.ServiceProvider = t.Services;
            LdtJudge.DataEngine.CallInterface.CommHelp.AnalysisLabelClass.analysisLabelHelper = t.Services.GetService<AnalysisLabelHelper>();
            LdtJudge.DataEngine.CallInterface.CommHelp.AnalysisLabelClass.extract = t.Services.GetService<IExtract>();
            AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper;
            TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
            t.Run();
        }



        private static void UnhandledExceptionTrapper(object sender, UnhandledExceptionEventArgs e)
        {
            var ex = (e.ExceptionObject as Exception);
          
        }
        private static void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs ex)
        {
            ex.SetObserved();
        }

        /// <summary>
        /// 创建主机
        /// </summary>
        /// <param name="args">参数</param>
        /// <returns></returns>
        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((hostingContext, config) =>
                {
                    //重新指定配置文件基础目录
                    config.SetBasePath(System.AppContext.BaseDirectory);
                })
                .ConfigureServices((hostContext, services) =>
                {

                    string settingcenter = hostContext.Configuration["appSettings:setting_center_address"];
                    services.AddHttpClient().AddSingleton<HttpHelper>();
                    services.AddSingleton<ISettingCenter>(
                        con =>
                        {
                            return new SettingCenterClient(settingcenter, "LDT71_WordParseAPI", con.GetService<HttpHelper>());
                        }
                    );
                    services.AddSingleton<FreesqlDb>();
                    services.AddSingleton(x => new AnalysisLabelHelper(settingcenter));
                    services.AddSingleton<IExtract, Extract>();
                   
                    services.AddHostedService<Worker>();
                    

                }).UseWindowsService();

    }
}
